package cn.studyjams.s2.sj0110.what2eat;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.google.firebase.analytics.FirebaseAnalytics;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    // 实物
    private GridView foodGrid;
    private SimpleAdapter foodAdapter;
    private List<Map<String,Object>> data_list;
    private String[] name_arr = {"水饺","炒饭","锅盖面","盒饭","不吃了","火锅","拉面","烧烤","煲仔饭"};
    private int[] img_arr = {
            R.drawable.shuijiao,R.drawable.chaofan, R.drawable.guogaimian,
            R.drawable.hefan,R.drawable.buchi,R.drawable.huoguo,
            R.drawable.lamian, R.drawable.shaokao,R.drawable.baozaifan
    };

    private FirebaseAnalytics mFirebaseAnalytics;

    // 当前对象
    private MainActivity mContent;

    // 开始 按钮
    private Button startBtn;
    // 转盘是否在转
    private boolean isRunning = false;

    private int prevBoxIndex = 0;
    private int currentBoxIndex = 0;

    private Handler handler = null;

    private int[] router = {0,1,2,5,8,7,6,3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContent = this;
        foodGrid = (GridView) findViewById(R.id.food_grid);
        getData();
        foodAdapter = new SimpleAdapter(this,data_list,R.layout.food_grid_item,
                new String[]{"food_name","food_img"},
                new int[]{R.id.food_name,R.id.food_img});

        foodGrid.setAdapter(foodAdapter);

        // Obtain the FirebaseAnalytics instance.
        mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);

        //点击按钮
        startBtn = (Button) findViewById(R.id.start_btn);
        startBtn.setOnClickListener(this);

        handler = new Handler();

    }

    /**
     * 获取适配器数据
     * @return List
     */
    private List<Map<String,Object>> getData(){
        data_list = new ArrayList<Map<String, Object>>();
        for (int i = 0;i < 9; i++){
            Map<String,Object> map = new HashMap<String, Object>();
            Log.i("food_name",name_arr[i]);
            map.put("food_name",name_arr[i]);
            map.put("food_img",img_arr[i]);
            data_list.add(map);
        }
        return data_list;
    }


    @Override
    public void onClick(View v) {
        String tip = null;
        if(this.isRunning){
            tip = "已经开始了";
        }else {

            Bundle bundle = new Bundle();
            bundle.putString("time", String.valueOf(System.currentTimeMillis()) );
            mFirebaseAnalytics.logEvent("click", bundle);

            tip = "开始选择了";
            this.isRunning = true;
            new Thread(new Runnable() {

                // 一共变化的次数
                private int l;
                // 当前变化的次数
                private int i = 0;

                @Override
                public void run() {
                    try {
                        l = (int) (Math.random() * 10) + 36;
                        Log.i("eat","开始了"+String.valueOf(l));
                        int half = l / 2;
                        for ( ; i < l; i++){

                            Thread.sleep((long) (Math.sin((i - half) / half * Math.PI) )* 500 + 200);
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Log.i("eat","变化背景"+currentBoxIndex);
                                    //把上一个 背景换成 空
                                    LinearLayout prev = (LinearLayout) foodGrid.getChildAt( prevBoxIndex );
                                    LinearLayout current = (LinearLayout) foodGrid.getChildAt(currentBoxIndex);
                                    prev.setBackgroundColor(0);
                                    current.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                                    prevBoxIndex = currentBoxIndex;
                                    currentBoxIndex = router[ i % 8];
                                }
                            });
                        }
                        Log.i("eat-ok","选中的是"+currentBoxIndex);

                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                isRunning = false;
                                Log.i("eat-jump","开始跳转");
                                // 跳转显示结果
                                startBtn.setText("再选一次");
                                Intent intent = new Intent(mContent,Result.class);
                                intent.putExtra("img",img_arr[prevBoxIndex]);
                                intent.putExtra("name",name_arr[prevBoxIndex]);
                                mContent.startActivity(intent);
                            }
                        });

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        Log.i("eat","InterruptedException");
                    }
                }
            }).start();
        }
        Toast.makeText(this,tip,Toast.LENGTH_SHORT).show();
    }


}
